package com.jasmine.auth;


import com.jasmine.auth.model.Token;

/**
 * token上下文,方便线程内使用对象
 *
 * @author jasmineXz
 */
public final class TokenContextHolder {
    private static final ThreadLocal<Token> contextHolder = new ThreadLocal<>();

    public static Token getContext (){
        Token token = contextHolder.get();

        if (token == null) {
            token = createEmptyContext();
            contextHolder.set(token);
        }

        return token;
    }

    public static void setContext (Token token) {
        contextHolder.set(token);
    }

    public static void removeContext () {
        contextHolder.remove();
    }

    private static Token createEmptyContext() {
        return new Token();
    }
}
